package com.ibeeking.found.open.b.rest.service.impl;

import com.alipay.v2.api.AlipayApiException;
import com.alipay.v2.api.AlipayClient;
import com.alipay.v2.api.AlipayConfig;
import com.alipay.v2.api.DefaultAlipayClient;
import com.ibeeking.found.common.entity.open.OpenAuthDTO;
import com.ibeeking.found.common.entity.open.OpenAuthQuery;
import com.ibeeking.found.common.entity.open.OpenConfigDTO;
import com.ibeeking.found.common.entity.open.OpenConfigQuery;
import com.ibeeking.found.common.enums.AliPayOpenAppTypeEnum;
import com.ibeeking.found.common.enums.OpenApiConfigEnum;
import com.ibeeking.found.common.enums.WxOpenAppTypeEnum;
import com.ibeeking.found.common.response.ResponseCodeEnum;
import com.ibeeking.found.common.utils.DataUtils;
import com.ibeeking.gmc.global.api.feign.GlobalOpenFeignClient;
import com.ibeeking.found.open.b.rest.config.prop.FileConfigProp;
import com.ibeeking.found.open.b.rest.config.prop.RedisProperties;
import com.ibeeking.found.open.b.rest.service.IAliPayOpenService;
import com.ibeeking.nematos.core.utils.SpringBeanUtil;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.redis.utils.RedisUtils;
import com.ibeeking.nematos.utils.result.ResponseResult;
import me.chanjar.weixin.open.api.WxOpenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 禁止在此类加其他业务方法
 *
 * @ClassName WxMpService
 * @Description
 * @Author ibeeking
 * @Date 2020-12-30 10:29
 **/
@Service
@EnableConfigurationProperties({RedisProperties.class})
public class AliPayOpenServiceImpl implements IAliPayOpenService {
    private Logger logger = LoggerFactory.getLogger(getClass());
    //30分钟
    private static final Long TIME = 30 * 60 *1000L;

    @Resource
    private RedisUtils redisUtils;

    @Autowired
    private FileConfigProp fileConfigProp;

    @Autowired
    private RedisProperties redisProperties;

    @Resource
    private GlobalOpenFeignClient globalOpenFeignClient;

    private OpenConfigDTO queryOpenConfig(Integer type, Integer subType) {
        OpenConfigQuery query = new OpenConfigQuery();
        query.setType(type);
        query.setSubType(subType);
        ResponseResult<OpenConfigDTO> responseResult = globalOpenFeignClient.queryOpenConfigOne(query);
        OpenConfigDTO openConfigDTO = responseResult.getData();
        return openConfigDTO;
    }

    private OpenAuthDTO queryAliPayOpenAuthByTenantIdAndType(Long tenantId, Integer appType){
        OpenAuthQuery query = new OpenAuthQuery();
        query.setTenantId(tenantId);
        query.setAppType(appType);
        ResponseResult<OpenAuthDTO> responseResult = globalOpenFeignClient.queryOpenAuthOne(query);
        return responseResult.getData();
    }

    private OpenAuthDTO queryAliPayOpenAuthByOpenAppIdAndType(String openAppId, Integer appType){
        OpenAuthQuery query = new OpenAuthQuery();
        query.setOpenAppId(openAppId);
        query.setAppType(appType);
        ResponseResult<OpenAuthDTO> responseResult = globalOpenFeignClient.queryOpenAuthOne(query);
        return responseResult.getData();
    }

    private OpenAuthDTO queryAliPayOpenAuthByComponentAppIdAndAppId(String componentAPPId, String appId){
        OpenAuthQuery query = new OpenAuthQuery();
        query.setComponentAppId(componentAPPId);
        query.setAppId(appId);
        ResponseResult<OpenAuthDTO> responseResult = globalOpenFeignClient.queryOpenAuthOne(query);
        return responseResult.getData();
    }

    @Override
    public  OpenConfigDTO queryOpenConfigByComponentAppId(String componentAppId){
        OpenConfigQuery query = new OpenConfigQuery();
        query.setComponentAppId(componentAppId);
        query.setType(OpenApiConfigEnum.OPEN_CONFIG_TYPE_ALIPAY_OPEN.getCode());
        query.setSubType(AliPayOpenAppTypeEnum.ALIPAY_OPEN_APP_TYPE_TP.getCode());
        ResponseResult<OpenConfigDTO> responseResult = globalOpenFeignClient.queryOpenConfigOne(query);
        DataUtils.trueThrowException(!ResponseCodeEnum.SUCCESS.getCode().equals(responseResult.getCode()), responseResult.getMsg());
        return responseResult.getData();
    }
    /**
     * 创建支付宝第三方应用服务
     *
     * @param
     * @return
     */
    @Override
    public AlipayClient getDefaultAliPayTpClient()   {
        try {
            OpenConfigDTO openConfigDTO = this.queryOpenConfig(OpenApiConfigEnum.OPEN_CONFIG_TYPE_ALIPAY_OPEN.getCode(), AliPayOpenAppTypeEnum.ALIPAY_OPEN_APP_TYPE_TP.getCode());
            AlipayConfig alipayConfig = new AlipayConfig();
            alipayConfig.setAppId(openConfigDTO.getComponentAppId());
            alipayConfig.setAlipayPublicKey(openConfigDTO.getOpenAppSecret());
            alipayConfig.setPrivateKey(openConfigDTO.getComponentSecret());
            AlipayClient alipayClient  = new DefaultAlipayClient(alipayConfig);
            if(alipayClient == null) throw new BusinessException("get AlipayClient service error!");
            return alipayClient;
        } catch (AlipayApiException e) {
            throw new BusinessException("get AlipayClient service error!");
        }
    }
    /**
     * 创建支付宝移动应用服务
     *
     * @param
     * @return
     */
    @Override
    public AlipayClient getDefaultAliPayAppClient()   {
        try {
            OpenConfigDTO openConfigDTO = this.queryOpenConfig(OpenApiConfigEnum.OPEN_CONFIG_TYPE_ALIPAY_OPEN.getCode(), AliPayOpenAppTypeEnum.ALIPAY_OPEN_APP_TYPE_APP.getCode());
            AlipayConfig alipayConfig = new AlipayConfig();
            alipayConfig.setAppId(openConfigDTO.getComponentAppId());
            alipayConfig.setAlipayPublicKey(openConfigDTO.getOpenAppSecret());
            alipayConfig.setPrivateKey(openConfigDTO.getComponentSecret());
            AlipayClient alipayClient  = new DefaultAlipayClient(alipayConfig);
            if(alipayClient == null) throw new BusinessException("get AlipayClient service error!");
            return alipayClient;
        } catch (AlipayApiException e) {
            throw new BusinessException("get AlipayClient service error!");
        }
    }
    /**
     * 创建支付宝网页应用服务
     *
     * @param
     * @return
     */
    @Override
    public AlipayClient getDefaultAliPayWebClient()   {
        try {
            OpenConfigDTO openConfigDTO = this.queryOpenConfig(OpenApiConfigEnum.OPEN_CONFIG_TYPE_ALIPAY_OPEN.getCode(), AliPayOpenAppTypeEnum.ALIPAY_OPEN_APP_TYPE_WEB.getCode());
            AlipayConfig alipayConfig = new AlipayConfig();
            alipayConfig.setAppId(openConfigDTO.getComponentAppId());
            alipayConfig.setAlipayPublicKey(openConfigDTO.getOpenAppSecret());
            alipayConfig.setPrivateKey(openConfigDTO.getComponentSecret());
            AlipayClient alipayClient  = new DefaultAlipayClient(alipayConfig);
            if(alipayClient == null) throw new BusinessException("get AlipayClient service error!");
            return alipayClient;
        } catch (AlipayApiException e) {
            throw new BusinessException("get AlipayClient service error!");
        }
    }
    /**
     * 创建支付宝生活号应用服务
     *
     * @param
     * @return
     */
    @Override
    public AlipayClient getDefaultAliPayMpClient()   {
        try {
            OpenConfigDTO openConfigDTO = this.queryOpenConfig(OpenApiConfigEnum.OPEN_CONFIG_TYPE_ALIPAY_OPEN.getCode(), AliPayOpenAppTypeEnum.ALIPAY_OPEN_APP_TYPE_MP.getCode());
            AlipayConfig alipayConfig = new AlipayConfig();
            alipayConfig.setAppId(openConfigDTO.getComponentAppId());
            alipayConfig.setAlipayPublicKey(openConfigDTO.getOpenAppSecret());
            alipayConfig.setPrivateKey(openConfigDTO.getComponentSecret());
            AlipayClient alipayClient  = new DefaultAlipayClient(alipayConfig);
            if(alipayClient == null) throw new BusinessException("get AlipayClient service error!");
            return alipayClient;
        } catch (AlipayApiException e) {
            throw new BusinessException("get AlipayClient service error!");
        }
    }
    /**
     * 创建支付宝小程序应用服务
     *
     * @param
     * @return
     */
    @Override
    public AlipayClient getDefaultAliPayMaClient()   {
        try {
            OpenConfigDTO openConfigDTO = this.queryOpenConfig(OpenApiConfigEnum.OPEN_CONFIG_TYPE_ALIPAY_OPEN.getCode(), AliPayOpenAppTypeEnum.ALIPAY_OPEN_APP_TYPE_MA.getCode());
            AlipayConfig alipayConfig = new AlipayConfig();
            alipayConfig.setAppId(openConfigDTO.getComponentAppId());
            alipayConfig.setAlipayPublicKey(openConfigDTO.getOpenAppSecret());
            alipayConfig.setPrivateKey(openConfigDTO.getComponentSecret());
            AlipayClient alipayClient  = new DefaultAlipayClient(alipayConfig);
            if(alipayClient == null) throw new BusinessException("get AlipayClient service error!");
            return alipayClient;
        } catch (AlipayApiException e) {
            throw new BusinessException("get AlipayClient service error!");
        }
    }

    private AliPayOpenServiceImpl getBean() {
        return SpringBeanUtil.getBean(this.getClass());
    }
}
